package v1

import (
	"usplatformserver/internal/model"
	"usplatformserver/internal/service"
	"usplatformserver/pkg/app"
	"usplatformserver/pkg/errcode"

	"github.com/gin-gonic/gin"
)

// @Summer 获取图片列表
// @Produce json
// @param organType query int true "检查类型"
// @Param patientStamp query string false "患者时间戳" maxlength(100)
// @Success 200 {object} model.PatientImageListSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/patient/images [get]
func (p Patient) GetImageList(c *gin.Context) {
	param := service.PatientImageListRequest{}
	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	svc := service.New(c.Request.Context())

	if param.PatientStamp == "" {
		//查询最新患者信息
		patientInfo, err := svc.LastPatient()
		if err != nil {
			response.ToErrorResponse(errcode.ErrorGetLastPatientInfoFail)
			return
		}
		param.PatientStamp = patientInfo.TimeStamp
	}

	images, err := svc.GetPatientImageList(&param)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetPatientImageListFail)
		return
	}
	response.ToResponseListWithoutPager(images)
}

// @Summer 获取图片组
// @Produce json
// @Param patientStamp query string false "患者时间戳" maxlength(100)
// @Param organType query int true "检查类型"
// @Param imageId query int true "图片ID"
// @Param imageType query int true "图片类型"
// @Param boxId query int true "图片组id"
// @Success 200 {object} model.PatientImageListSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/patient/image/group [get]
func (p Patient) GetImageGroup(c *gin.Context) {
	param := service.PatientImageGroupRequest{}
	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	svc := service.New(c.Request.Context())

	if param.PatientStamp == "" {
		//查询最新患者信息
		patientInfo, err := svc.LastPatient()
		if err != nil {
			response.ToErrorResponse(errcode.ErrorGetLastPatientInfoFail)
			return
		}
		param.PatientStamp = patientInfo.TimeStamp
	}

	images, err := svc.GetImageGroup(&param)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetPatientImageGroupFail)
		return
	}
	response.ToResponseListWithoutPager(images)
}

// @Summer 获取图片列表（按时间倒序）
// @Produce json
// @param organType query int true "检查类型"
// @Param patientStamp query string false "患者时间戳" maxlength(100)
// @Success 200 {object} model.PatientImageListSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/patient/images/time/desc [get]
func (p Patient) GetImageListDescByTime(c *gin.Context) {
	param := service.PatientImageListRequest{}
	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	svc := service.New(c.Request.Context())

	if param.PatientStamp == "" {
		//查询最新患者信息
		patientInfo, err := svc.LastPatient()
		if err != nil {
			response.ToErrorResponse(errcode.ErrorGetLastPatientInfoFail)
			return
		}
		param.PatientStamp = patientInfo.TimeStamp
	}

	var images []*model.PatientImageInfo = nil
	var err error = nil
	if *param.OrganType == model.CheckpointThyroid {
		images, err = svc.GetThyroidImageListDescByTime(&param)
	} else if *param.OrganType == model.CheckpointBreast {
		images, err = svc.GetBreastImageListDescByTime(&param)
	}

	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetPatientImageListDescByTimeFail)
		return
	}
	response.ToResponseListWithoutPager(images)
}
